<?php
/*
  $Id: gift_cards.php,v 1.0 2007/10/22 by Kasey Drexler $
  Email: kdrexler@dyton.com or x@findyourownjob.com

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
  
  // check to seee if we have a valid gift card
  if (isset($HTTP_GET_VARS['gcID']) && is_numeric($HTTP_GET_VARS['gcID'])) {
  
    // now check the gcID to make sure the purchaser is the customer logged in
	$card_check = tep_db_query("select * from `".TABLE_GIFT_CARDS."` where purchaser='".(int)$_SESSION['customer_id']."' and gift_cards_id='".(int)$HTTP_GET_VARS['gcID']."'");
	if (tep_db_num_rows($card_check)>0) {
	  $card = tep_db_fetch_array($card_check);
	  
	  // grab user information to send in email
	  $user_query = tep_db_query("select * from `".TABLE_CUSTOMERS."` where customers_id='".(int)$_SESSION['customer_id']."'");
	  $user = tep_db_fetch_array($user_query);
	  
	} else {
	  tep_redirect(tep_href_link(FILENAME_GIFT_CARDS, '', 'SSL'));
	}
  } else {
    tep_redirect(tep_href_link(FILENAME_GIFT_CARDS, '', 'SSL'));
  }
  
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_GIFTCARD_EMAIL);
  
  // send out email if we are ready to
  if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'process')) {
    $code = tep_db_prepare_input($HTTP_POST_VARS['gc_code']);
	$amount = tep_db_prepare_input($HTTP_POST_VARS['gc_amount_remaining']);
	$sending_user = tep_db_prepare_input($HTTP_POST_VARS['customer_name']);
	$to_address = tep_db_prepare_input($HTTP_POST_VARS['email_address']);
	$v_address = tep_db_prepare_input($HTTP_POST_VARS['verify_address']);
	
	// check inputs
	$error = false;
	if (($code == '') || ($amount == '') || ($sending_user == '')) {
	  $error = true;
	  $messageStack->add('email_gc', ERROR_CARD_ERROR);
	}
	
	if ($v_address != $to_address) {
	  $error = true;
	  $messageStack->add('email_gc', ERROR_EMAIL_MATCH);
	}
	
	if (($to_address == '') || (!tep_validate_email($to_address))) {
	  $error = true;
	  $messageStack->add('email_gc', ERROR_EMAIL_ERROR);
	}
	
	if (!$error) { // if no error then send the person an email
	  
	  $email_text = sprintf(EMAIL_TEXT, $sending_user, $code, $amount);
	  
	  $email_text = $email_text . EMAIL_CONTACT . EMAIL_WARNING;
	  
	  tep_mail('', $to_address, EMAIL_SUBJECT_BODY, $email_text, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
	  
	  $messageStack->add('email_gc', SUCCESS_EMAIL_SENT, 'success');
	}
  }

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_GIFTCARD_EMAIL, '', 'SSL'));
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script language="JavaScript" src="includes/functions.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="3" cellpadding="3">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2" class="left_nav">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php //echo tep_image(DIR_WS_IMAGES . 'table_background_account.gif', HEADING_TITLE, HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
<?php
  if ($messageStack->size('email_gc') > 0) {
?>
      <tr>
        <td><?php echo $messageStack->output('email_gc'); ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
<?php
  }
?>
	  <tr>
	    <td><p><?php echo TEXT_BODY; ?></p></td>
	  </tr>
	  <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
	    <td>
		<?php echo tep_draw_form('email_gift_card', tep_href_link(FILENAME_GIFTCARD_EMAIL, 'action=process&gcID='.$card['gift_cards_id'], 'SSL'), 'post'); ?>
		<?php echo tep_draw_hidden_field('gc_code', $card['gift_cards_code']); ?>
		<?php echo tep_draw_hidden_field('gc_amount_remaining', $card['gift_cards_amount_remaining']); ?>
		<?php echo tep_draw_hidden_field('customer_name', $user['customers_firstname'].' '.$user['customers_lastname']); ?>
		
		<table border="0" cellpadding="0" cellspacing="5">
		  <tr>
		    <td><?php echo FIELD_EMAIL; ?></td>
			<td><input type="text" name="email_address" /></td>
		  </tr>
		  <tr>
		    <td align="right"><?php echo FIELD_VERIFY_EMAIL; ?></td>
			<td><input type="text" name="verify_address" /></td>
		  </tr>
		  <tr>
		    <td align="right"><a href="<?php echo tep_href_link(FILENAME_GIFT_CARDS); ?>">< Go Back</a></td><td align="left"><?php echo dyton_draw_submit('Send', 'class="page_buttons"'); ?></td>
		  </tr>
		</table>
		</form>
		</td>
	  </tr>
    </table></td>
<!-- body_text_eof //-->
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- right_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_right.php'); ?>
<!-- right_navigation_eof //-->
    </table></td>
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
